﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OreSystem.BLL;
using OreSystem.Entities;
namespace MainFrom
{
    public partial class ExcvavtorEditForm : Form
    {
        private CarBLL carBLL = new CarBLL();
        private LevelBll levelBll = new LevelBll();
        public IList<Level> Plevels { get; set; }
        public Car newCar { get; set; }
       
        //private bool newCarFlag = true;
        public ExcvavtorEditForm()
        {
            InitializeComponent();
        }

        private void ExcvavtorEditForm_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            lbl_message.ForeColor = Color.Red;
            cmb_Pid.DisplayMember = "Name";
            cmb_Pid.ValueMember = "ID";
            cmb_Pid.DataSource = Plevels;
            if (newCar !=null)
            {
                //newCarFlag = false;
                txt_CarNo.Text = newCar.CarNo;
                //cmb_Pid.SelectedItem = levelBll.GetLevelById(newCar.PID);
                cmb_Pid.SelectedValue = newCar.PID;

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (cmb_Pid.SelectedItem == null) {
                lbl_message.Text = "车上层不能为空！";
                cmb_Pid.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txt_CarNo.Text))
            {
                lbl_message.Text = "车编号不能为空！";
                txt_CarNo.Focus();
                return;
            }
           
            //newCar.PID = (int)cmb_Pid.SelectedValue;
            //newCar.CarNo = txt_CarNo.Text;
            //newCar.Cubic = 0;
            //newCar.LicensePlate = "";
            //newCar.Type = 2;
            //newCar.CreateDate = DateTime.Now;
            try {
                if (newCar == null)
                {
                    carBLL.Save(new Car { PID = (int)cmb_Pid.SelectedValue, LicensePlate = "", Cubic = 0, CreateDate = DateTime.Now, Type = 2, CarNo = txt_CarNo.Text});
                    lbl_message.Text = "信息保存成功";
                    Reset();
                }
                else
                {
                    newCar.PID = (int)cmb_Pid.SelectedValue;
                    newCar.CarNo = txt_CarNo.Text;
                    newCar.Cubic = 0;
                    newCar.LicensePlate = "";
                    newCar.Type = 2;
                    newCar.CreateDate = DateTime.Now;
                    carBLL.Save(newCar);
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }

                //carBLL.Save(newCar);
                //lbl_message.Text = "信息保存成功";
                //Reset();
                //if (newCarFlag) {
                //    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                //    this.Close();
                //}
                
            }
            catch (Exception ex) {

                MessageBox.Show("出现错误请联系管理员。错误信息" + ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e) {
            Reset();
        }
        private void Reset() {
            txt_CarNo.Text = "";
            cmb_Pid.Focus();
        }
        
        private void cmb_Pid_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == (char)13) {
                txt_CarNo.Focus();
            }
        }

        private void txt_CarNo_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == (char)13) {
                button1.Focus();
            }
        }
    }
}
